<?php
    // new cat
    class NewsMode
    {
        public function __construct(PDO $pdo) {
            $this->pdo = $pdo;
        }
        public function getAllNews()
        {
            $sql = "SELECT * FROM set_news";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;            
        }
        public function getNewsByID($id)
        {
            $sql = "SELECT * FROM set_news WHERE ID_NEWS=$id limit 1";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetch();
            return $result;            
        }
        public function insertNews($arrayCat)
        {
            $nameUrlSeo = cleanUpSpecialChars(removeSpecialChars(removesign($arrayCat['NAME_NEWS'])));
            $sql = "INSERT INTO set_news (
                NAME_NEWS,
                IDCAT_NEWS,
               	NAMESEO_NEWS,
               	URL_NEWS,
                DES_NEWS,
                KEYWORD_NEWS,
                PHOTO_NEWS,
                SUMARY_NEWS,
                CONTENT_NEWS,
                TAGS_NEWS,
                HOT_NEWS,
                STATUS_NEWS
                ) 
                VALUES (
                :NAME_NEWS,
                :IDCAT_NEWS,
               	:NAMESEO_NEWS,
               	:URL_NEWS,
                :DES_NEWS,
                :KEYWORD_NEWS,
                :PHOTO_NEWS,
                :SUMARY_NEWS,
                :CONTENT_NEWS,
                :TAGS_NEWS,
                :HOT_NEWS,
                :STATUS_NEWS
                )";
            $sth = $this->pdo->prepare($sql);
            $result = $sth->execute(array(
                                ':NAME_NEWS'                =>$arrayCat['NAME_NEWS'],
                                ':IDCAT_NEWS'               =>$arrayCat['IDCAT_NEWS'],
                                ':NAMESEO_NEWS'             =>$arrayCat['NAMESEO_NEWS'],
                                ':URL_NEWS'                 =>$nameUrlSeo,
                                ':DES_NEWS'                 =>$arrayCat['DES_NEWS'],
                                ':KEYWORD_NEWS'             =>$arrayCat['KEYWORD_NEWS'],
                                ':PHOTO_NEWS'               =>$arrayCat['PHOTO_NEWS'],
                                ':SUMARY_NEWS'              =>$arrayCat['SUMARY_NEWS'],
                                ':CONTENT_NEWS'             =>$arrayCat['CONTENT_NEWS'],
                                ':TAGS_NEWS'                =>$arrayCat['TAGS_NEWS'],
                                ':HOT_NEWS'                 =>isset($arrayCat['HOT_NEWS'])?1:0,
                                ':STATUS_NEWS'              =>isset($arrayCat['STATUS_NEWS'])?1:0,                         
                        ));
                            
            return true;             
        }
        
        public function editNews($arrayCat)
        {
            $nameUrlSeo = cleanUpSpecialChars(removeSpecialChars(removesign($arrayCat['NAME_NEWS'])));
            $sql = "UPDATE set_news 
                    SET IDCAT_NEWS=?, NAME_NEWS=?, NAMESEO_NEWS=?, URL_NEWS=?,
                   	DES_NEWS=?, KEYWORD_NEWS=?,PHOTO_NEWS=?,SUMARY_NEWS=?, CONTENT_NEWS=?,
                    TAGS_NEWS=?, HOT_NEWS=?, STATUS_NEWS=?
            		WHERE ID_NEWS=?";
            $sth = $this->pdo->prepare($sql);
            $result = $sth->execute(array($arrayCat['IDCAT_NEWS'],$arrayCat['NAME_NEWS'],$arrayCat['NAMESEO_NEWS'],$nameUrlSeo,
            $arrayCat['DES_NEWS'],$arrayCat['KEYWORD_NEWS'],$arrayCat['PHOTO_NEWS'],$arrayCat['SUMARY_NEWS'],$arrayCat['CONTENT_NEWS'],
            $arrayCat['TAGS_NEWS'], isset($arrayCat['HOT_NEWS'])?1:0, isset($arrayCat['STATUS_NEWS'])?1:0,$arrayCat['ID_NEWS']));               
            return true;            
        }
        function deleteNews($idPro){
            $sqlPro = "DELETE FROM `set_news` WHERE `ID_NEWS` = $idPro";
            $sthPro = $this->pdo->prepare($sqlPro);
            $sthPro->execute(); 
        }        
    }
